package com.jxc.servlet.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie/IndexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        // requests.getCookies()用于获取所有的以前保存的cookie信息
        Cookie[] cookie = req.getCookies();
        for (Cookie cs:cookie){
//            resp.getWriter().print("<h1>cookie name:"+cs.getName()+"</h1>");
//            resp.getWriter().print("<h1>cookie value:"+cs.getValue()+"</h1>");
            String user = null;
            if (cs.getName().equals("user")){
                user = cs.getValue();
                break;
            }
            if (user != null){
                resp.getWriter().print("<h1> username:"+user+"</h1>");
            }else {
                resp.getWriter().print("<h1>用户登录失败</h1>");
            }

        }
    }
}
